package com.frejoys.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frejoys.common.dto.admin.StoreIdAndPageDto;
import com.frejoys.common.dto.admin.StoreIdAndStoreActivityIdDto;
import com.frejoys.common.vo.admin.StoreActivityQueueListVo;
import com.frejoys.common.vo.store.StoreActivityQueueVo;
import com.frejoys.dao.entity.StoreActivityQueue;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

public interface StoreActivityQueueMapper extends BaseMapper<StoreActivityQueue> {

    // 添加队列
    @Insert("insert into j_store_activity_queue (user_id,store_activity_id,order_sn,price,rebate_money,`type`,create_time,status) values (#{userId}, #{storeActivityId}, #{orderSn}, #{price}, #{rebateMoney}, #{type}, #{createTime}, #{status})")
    Boolean addQueue(@Param("userId") Long userId, @Param("storeActivityId") Integer storeActivityId, @Param("orderSn") Long orderSn, @Param("price") BigDecimal price, @Param("rebateMoney") BigDecimal rebateMoney, @Param("type") Integer type, @Param("createTime") Integer createTime, @Param("status") Integer status);

    // 更新排队状态
    @Update("update j_store_activity_queue set rebate_money=rebate_money+#{rebateMoney},status=#{newStatus} where order_sn=#{orderSn} and status=#{oldStatus}")
    boolean upQueueStatusAndRebateMoney(@Param("orderSn") Long orderSn, @Param("oldStatus") Integer oldStatus, @Param("newStatus") Integer newStatus, @Param("rebateMoney") BigDecimal rebateMoney);

    // 更新排队金额
    @Update("update j_store_activity_queue set rebate_money=rebate_money+#{rebateMoney} where order_sn=#{orderSn} and status=#{oldStatus}")
    boolean upRebateMoney(@Param("orderSn") Long orderSn, @Param("oldStatus") Integer oldStatus, @Param("rebateMoney") BigDecimal rebateMoney);


    //查询店铺活动
    // @Select("select user_id, store_activity_id, price,type,create_time,status from j_store_activity_queue where status > 0")
    // List<StoreActivityQueue> getActivityList();

    //根据用户id,活动id,订单id查询店铺活动
    @Select("select id,store_activity_id,price,type,create_time,status from j_store_activity_queue where order_sn = #{orderSn} and user_id = #{userId} and store_activity_id = #{storeActivityId} and status > 0  limit 1")
    StoreActivityQueueVo getActivity(@Param("userId") Long userId, @Param("orderSn") Long orderSn, @Param("storeActivityId")Integer storeActivityId);

    //查询活动队列列表
    IPage<StoreActivityQueueListVo> activityQueueListInfo(@Param("page") Page page, @Param("ids") List<Integer> ids, @Param("dto") StoreIdAndStoreActivityIdDto dto, @Param("userId") Long userId, @Param("storeActivityIdList") List<Integer> storeActivityIdList);

    //获取订单排序
    Integer getNowSort(@Param("storeActivityId") Integer storeActivityId,@Param("orderSn") Long orderSn,@Param("status") Integer status);
}
